package business;

import java.io.Serializable;
import java.util.LinkedList;

public class Project implements Serializable
{

    /* Resources Lists */
    private LinkedList<AbstractDocument> documentsList;
    private LinkedList<String> titlesList;

    /* Basic Attributes */
    private String name;
    private String type;

    /* Constructors */
    public Project(String name, String type)
    {
        this.name = name;
        this.type = type;
        documentsList = new LinkedList();
        titlesList = new LinkedList();
    }

    /* Checking Methods */
    public boolean checkDocumentsList(AbstractDocument newFile)
    {
        if (documentsList.contains(newFile))
        {
            return false;
        }

        return true;
    }

    public boolean checkFileName(String newFileName)
    {
        if (newFileName.equals(""))
        {
            return false;
        }
        if (newFileName.startsWith(" "))
        {
            return false;
        }

        return true;
    }

    /* Adding Methods */
    public void addFile(AbstractDocument file)
    {
        documentsList.add(file);
        titlesList.add(file.getTitle());
    }
    
    public AbstractDocument newFile(String title) {
        AbstractDocument doc = null;
        switch (type) {
            case "C": doc = new CDocument(title, "");
                      break;
            case "Java": doc = new JavaDocument(title, "");
                      break;
            case "Txt": doc = new TxtDocument(title, "");
                        break;
        }
        
        return doc;
    }

    /* Removal Method */
    public void remove(String oldFileName)
    {
        for (int i = 0; i < documentsList.size(); i++)
        {
            if (documentsList.get(i).getTitle().equals(oldFileName))
            {
                documentsList.remove(i);
                break;
            }
        }
        for (int i = 0; i < titlesList.size(); i++)
        {
            if (titlesList.get(i).equals(oldFileName))
            {
                titlesList.remove(i);
                break;
            }
        }
    }

    /**
     * Get all names
     *
     * @return array of names of files
     */
    public String[] getNames()
    {
        String names[];

        if (titlesList.isEmpty())
        {
            names = new String[1];
            names[0] = "";
        }
        else
        {
            names = new String[titlesList.size()];
            for (String newName : titlesList)
            {
                names[titlesList.indexOf(newName)] = newName;
            }
        }

        return names;
    }

    public AbstractDocument[] getDocuments()
    {
        AbstractDocument docs[];

        if (documentsList.isEmpty())
        {
            docs = null;
        }
        else
        {
            docs = new AbstractDocument[documentsList.size()];
            for (AbstractDocument newDoc : documentsList)
            {
                docs[documentsList.indexOf(newDoc)] = newDoc;
            }
        }

        return docs;
    }

    /* Search Method */
    public AbstractDocument getDocumentByName(String oldFileName)
    {
        for (AbstractDocument olDoc : documentsList)
        {
            if (olDoc.getTitle().equals(oldFileName))
            {
                return olDoc;
            }
        }

        return null;
    }
}
